<template>
	<view class="hbh_container2">
		<headComponent navTitle="注册" :isBack="true"></headComponent>
		<view class="hbh_box">
			<view class="navBar">
				<view class="active">
					注册
				</view>
			</view>
			<SendCode defaultValue="" :maxlength="8" :inpType="'text'" :placeholder="'请输入您的用户名称'"
				@sendValue="sendCode2">
			</SendCode>
			<SendCode :maxlength="11" :inpType="'number'" :placeholder="'请输入您的手机号'" @sendValue="sendCode3">
			</SendCode>
			<SendCode :second="isSecond" :maxlength="6" :inpType="'number'" @putCode="putCode"
				:placeholder="'请输入验证码'" @sendValue="sendCode4" :isCode="true"></SendCode>
			<SendCode :password="true" :isSee="true" :placeholder="'请输入6位数及以上密码'" @sendValue="sendCode">
			</SendCode>
			<view class="radio">
				<radio-group name="radio" @change="radioChange">
					<label>
						<radio value="男" /><text>高富帅(男)</text>
					</label>
					<label>
						<radio value="女" /><text>白富美(女)</text>
					</label>
				</radio-group>
			</view>
			<view class="selectAge">
				<text>年龄几何:</text>
				<picker @change="bindPickerChange" :value="index" :range="array">
					<view class="uni-input">{{array[index]}}</view>
				</picker>
			</view>
			<view class="hbh_endBtn" @click="subBtn">
				注册
			</view>
		</view>
		<Createcode :titleName="'注册验证码为'" :code="localCode" v-if="isShowCode"></Createcode>
	</view>
</template>

<script>
	import SendCode from "@/components/hbhInput/hbhInput.vue"
	import Createcode from "@/components/Createcode/Createcode.vue"
	import {
		regeister,
		sendCode
	} from "@/api/index.js"
	export default {
		data() {
			return {
				pwd: "",
				phoneNumber: "",
				userName: "",
				code: "",
				sex: "",
				isFill: false,
				isShowCode: false,
				timer: null,
				localCode: "",
				isSecond: true,
				array: [],
				index: 18,
			};
		},
		components: {
			SendCode,
			Createcode
		},
		watch: {
			phoneNumber: {
				handler(newValue, oldValue) {
					if (newValue.length >= 11) {
						this.isSecond = false
						console.log(this.isSecond)
					} else {
						this.isSecond = true
					}
				}
			}
		},
		onLoad() {
			this.getAge()
		},
		methods: {
			//
			getAge() {
				let arr = []
				for (let i = 0; i < 100; i++) {
					arr.push(i + '岁')
				}
				this.array = arr
			},
			bindPickerChange: function(e) {
				this.index = e.detail.value
			},
			//选性别
			radioChange(e) {
				this.sex = e.detail.value
			},
			subBtn() {
				if (!this.pwd || !this.phoneNumber || !this.code || !this.userName || !this.sex) {
					uni.showToast({
						icon: "error",
						title: "表单填写错误"
					})
					return
				} else {
					if (this.code == this.localCode) {
						plus.device.getInfo({ //需要勾选IDFA
							success: (e) => {
								regeister({
									user_name: this.userName,
									phone: this.phoneNumber,
									sex: this.sex,
									age: this.index,
									pwd: this.pwd,
									uuid:e.uuid
								}).then((res) => {
									console.log(res)
									setTimeout(() => {
										uni.reLaunch({
											url: "/pages/login/login"
										});
									}, 1500)
									uni.showToast({
										icon: "success",
										title: res.msg,
										duration: 1000
									})

								}).catch((err) => {
									console.log(err)
									if (err.code && err.code == 400) {
										uni.showToast({
											icon: "none",
											title: err.msg
										})
									} else {
										uni.showToast({
											icon: "none",
											title: "表单填写异常!"
										})
									}

								})
							},
							fail: function(e) {
								console.log('getDeviceInfo failed: ' + JSON.stringify(e));
							}
						});
					} else {
						uni.showToast({
							icon: "error",
							title: "验证码错误"
						})
					}

				}
			},
			//接收验证码
			putCode(val) {
				this.localCode = val
				this.isShowCode = true
				this.timer = setTimeout(() => {
					this.isShowCode = false
					clearTimeout(this.timer)
					this.timer = null
				}, 10000)
			},
			sendCode(val) {
				this.pwd = val
			},
			sendCode2(val) {
				this.userName = val
			},
			sendCode3(val) {
				this.phoneNumber = val
			},
			sendCode4(val) {
				this.code = val
			},
		}
	}
</script>

<style lang="scss" scoped>
	.selectAge {
		height: 100rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;

		picker {
			background-color: #f3f3f3;
			border-radius: 30rpx;
			flex: 1;
			height: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
		}
	}

	.radio {
		margin-bottom: 40rpx;
	}

	radio-group {
		display: flex;
		justify-content: space-between;
	}

	.navBar {
		margin-top: 122rpx;
		display: flex;
		margin-bottom: 114rpx;

		.active {
			text-align: center;
			display: flex;
			align-items: flex-end;
			font-size: 50rpx;
			font-weight: 500;
			color: #000000;
		}
	}

	.hbh_help {
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-size: 24rpx;
		font-weight: 400;
		color: #51C3FF;
	}

	.hbh_code {
		display: flex;
		justify-content: flex-end;
		align-items: center;
		font-size: 24rpx;
		font-weight: 400;
		color: #51C3FF;
	}

	.hbh_endBtn {
		width: 630rpx;
		margin-left: calc(50% - 315rpx);
		height: 90rpx;
		background: linear-gradient(90deg, #6ECDFF 0%, #00A7FF 100%);
		border-radius: 50rpx 50rpx 50rpx 50rpx;
		opacity: 1;
		font-size: 36rpx;
		font-weight: 500;
		color: #FFFFFF;
		text-align: center;
		line-height: 90rpx;
		margin-top: 100rpx;
	}
</style>